import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../components/Home.vue";
// import User from "../views/User.vue";

import { ElMessage } from 'element-plus'

const routes = [
  {
    path: "/",
    name: "home",
    component: Home,
    redirect: "/login",
    meta:{
      title:"首页"
    },
    children: [
      {
        path: "/welcome",
        name: "welcome",
        component: () => import("../components/Welcome.vue"),
        meta:{
          title:"欢迎页",
          requireAuth: true // 需要身份验证
        },
      },
      {
        path: "/user",
        name: "user",
        redirect: "/userlist",
        meta:{
          title:"用户管理",
          requireAuth: true // 需要身份验证
        },
        children: [
          {
              path: "/userlist",
              name: "用户列表",
              component: () => import("../components/user/userlist.vue"),
              meta:{
                title:"用户列表",
                requireAuth: true // 需要身份验证
              },
          },
        ],
        component:  () => import("../components/User.vue"),
      },
      {
        path: "/zhandian",
        name: "zhandian",
        redirect: "/menu",
        meta:{
          title:"站点管理",
          requireAuth: true // 需要身份验证
        },
        children: [
           {
              path: "/shezhi",
              name: "shezhi",
              component: () => import("../components/zhandian/shezhi.vue"),
              meta:{
                title:"站点设置",
                requireAuth: true // 需要身份验证
              },
          },
          {
              path: "/chucun",
              name: "chucun",
              component: () => import("../components/zhandian/chucun.vue"),
              meta:{
                title:"储存管理",       
                requireAuth: true // 需要身份验证
              },
          },
           {
              path: "/menu",
              name: "menu",
              component: () => import("../components/zhandian/menu.vue"),
              meta:{
                title:"菜单管理",
                requireAuth: true // 需要身份验证
              },
          },
           {
              path: "/danye",
              name: "danye",
              component: () => import("../components/zhandian/danye.vue"),
              meta:{
                title:"储存管理",
                requireAuth: true // 需要身份验证
              },
          },
           {
              path: "/lanmu",
              name: "lanmu",
              component: () => import("../components/zhandian/lanmu.vue"),
              meta:{
                title:"栏目管理",
                requireAuth: true // 需要身份验证
              },
          },
           {
              path: "/liandong",
              name: "liandong",
              component: () => import("../components/zhandian/liandong.vue"),
              meta:{
                title:"储存管理",
                requireAuth: true // 需要身份验证
              },
           },
        ],
        component: () => import("../components/Zhandian.vue"),

      },
      {
        path: "/manage",
        name: "manage",
        redirect: "/usermanage",
        meta:{
          title:"管理员管理",
          requireAuth: true // 需要身份验证
        },
        children: [
           {
              path: "/role",
              name: "role",
              meta:{
                title:"角色",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/manager/role.vue"),
          },
          {
              path: "/usermanage",
              name: "usermanage",
              meta:{
                title:"角色管理",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/manager/userMan.vue"),
            },
        ],
        component:  () => import("../components/Manager.vue"),
      },
      {
        path: "/cheliang",
        name: "car",
        redirect: "/carlist",
        meta:{
          title:"车辆管理",
          requireAuth: true // 需要身份验证
        },
        children: [
            {
              path: "/carlist",
              name: "carlist",
              meta:{
                title:"车辆列表",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/carlist.vue"),
          },
          {
              path: "/chuche",
              name: "chuche",
              meta:{
                title:"出车记录",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/chuche.vue"),
          },
          {
              path: "/jiayou",
              name: "加油",
              meta:{
                title:"加油记录",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/jiayou.vue"),
          },  
          {
              path: "/weixiu",
              name: "维修",
              meta:{
                title:"维修记录",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/weixiu.vue"),
          }, 
          {
              path: "/weizhang",
              name: "违章",
              meta:{
                title:"违章记录",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/weizhang.vue"),
          },  
          {
              path: "/qita",
              name: "qita",
              meta:{
                title:"其他记录",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/qita.vue"),
          },  
          {
              path: "/nianjian",
              name: "nianjian",
              meta:{
                title:"年检记录",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/nianjian.vue"),
          },
          {
              path: "/siji",
              name: "siji",
              meta:{
                title:"司机记录",
                requireAuth: true // 需要身份验证
              },
              component:  () => import("../components/carmanage/siji.vue"),
          },           
        ],
        component:  () => import("../components/Cheliang.vue"),
      },
      {
        path: "/caiwu",
        name: "caiwu",
        redirect: "/caiding",
        meta:{
          title:"休假申请",
          requireAuth: true // 需要身份验证
        },
        children: [
           {
             path: "/caiguan",
             name: "财务管理",
             meta:{
               title:"财务管理",
               requireAuth: true // 需要身份验证
             },
             component:  () => import("../components/caiwu/caiguan.vue"),
          },
          {
             path: "/caiding",
             name: "订单管理",
             meta:{
               title:"财务管理",
               requireAuth: true // 需要身份验证
             },
            //  component:  () => import("../components/caiwu/dingdan.vue"),
            component:  () => import("../components/carmanage/chuche.vue"),
           },
        ],
        component: () => import("../components/Caiwu.vue"),
      },
      {
        path: "/qrcode",
        name: "qrcode",
        redirect: "/qrmanage",
        meta:{
          title:"休假申请",
          requireAuth: true // 需要身份验证
        },
        children: [
           {
             path: "/qrmanage",
             name: "验证码管理",
             meta:{
               title:"验证码管理",
               requireAuth: true // 需要身份验证
             },
             component:  () => import("../components/qrcode/QRmanage.vue"),
          },
          {
             path: "/caiding",
             name: "订单管理",
             meta:{
               title:"财务管理",
               requireAuth: true // 需要身份验证
             },
            //  component:  () => import("../components/caiwu/dingdan.vue"),
            component:  () => import("../components/carmanage/chuche.vue"),
           },
        ],
        component: () => import("../components/QRcoe.vue"),
      },
    //   {
    //     path: "/approval/pending",
    //     name: "pending",
    //     meta:{
    //       title:"待我审批"
    //     },
    //     component: () => import("../views/Approval.vue"),
    //   }
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../view/Login.vue"),
  },
//   {
//     path: "/404",
//     name: "404",
//     component: () => import("../views/404.vue"),
//   },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 导航守卫
router.beforeEach((to, from, next) => {
  // 检查 localStorage 中是否存在用户登录状态
  const isLoggedIn = localStorage.getItem('token');


  if(to.meta.requireAuth){
      // 如果用户已经登录，放行，允许访问目标路由
      if (isLoggedIn) {
        next();
      } else {
        // 如果用户未登录，则重定向到登录页面
        ElMessage.error('请重新登录')
        next('/login');
      }
  }else{
    next()
  }

});
export default router;
